package com.hs.srm.admin.modules.bills.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hs.srm.base.common.annotation.SysLog;
import com.hs.srm.base.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hs.srm.admin.modules.bills.entity.ScmBillsEntity;
import com.hs.srm.admin.modules.bills.entity.ScmBillsHeaderEntity;
import com.hs.srm.admin.modules.bills.service.ScmBillsHeaderService;
import com.hs.srm.admin.modules.bills.service.ScmBillsService;
import com.hs.srm.admin.modules.orders.service.StockService;
import com.hs.srm.admin.modules.sys.controller.AbstractController;
import com.hs.srm.base.modules.sys.shiro.ShiroUtils;
import com.hs.srm.common.utils.PageUtils;
import com.hs.srm.common.utils.R;

/**
 * <p>標題：对账单controller层</p>
 * <p>描述：</p>
 * <p>版權：版權所有 (c) 2019</p>
 * <p>組織：厚声集团</p>
 *
 * @author 鐘曉籟
 * @version V1.0.0
 * @date 2019年4月2日 上午9:49:54
 * @since V1.0.0
 */
@RestController
@RequestMapping("/scmBillsHeader")
@Transactional
public class ScmBillsHeaderController extends AbstractController {

    /**
     * 类序列ID
     */
    private static final long serialVersionUID = -2392337998173025326L;

    @Autowired
    private StockService stockService;

    @Autowired
    private ScmBillsHeaderService scmBillsHeaderService;

    @Autowired
    private ScmBillsService scmBillsService;

    /**
     * 查询列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        SysUserEntity username = this.getUser();
        if (username.getDeptId() == 13) {
            params.put("username", username.getUsername());
        }
        PageUtils pu = this.scmBillsHeaderService.selectDetailPage(params);
        // List<ScmPoItemVo> voList = (List<ScmPoItemVo>)pu.getList();
        // voList = EntityUtils.valToTxt(voList);
        return R.ok(pu);
    }

    /**
     * 查询stock列表
     */
    @RequestMapping("/listStock")
    @ResponseBody
    public R listStock(@RequestParam Map<String, Object> params) {
        String status = (String) params.get("status");
        if (status != null) {
            String[] statusArr = status.split(",");
            params.put("status", statusArr);
        }
        PageUtils pu = this.stockService.selectDetailPage(params);
        // List<ScmPoItemVo> voList = (List<ScmPoItemVo>)pu.getList();
        // voList = EntityUtils.valToTxt(voList);
        return R.ok(pu);
    }

    /**
     * 生成对账单
     */
    @RequestMapping("/genBills")
    @ResponseBody
    public R genBills(@RequestParam("poIds[]") String[] poIds, @RequestParam("stockIds[]") String[] stockIds,
                      String vendorCode, BigDecimal sumMoney) {
        int c = this.scmBillsHeaderService.genBills(poIds, stockIds, vendorCode, sumMoney);
        return R.ok(c);
    }

    /**
     * 计算确认对账总金额
     */
    @RequestMapping("/checkSumMoney")
    @ResponseBody
    public R checkSumMoney(@RequestParam("stockIds[]") String[] stockIds) {
        BigDecimal sumMoney = this.scmBillsHeaderService.calSumMoney(stockIds);
        if (sumMoney.intValue() == -100) {
            R.error("入库单中没有订单信息或订单行项目信息");
        }
        if (sumMoney.intValue() == -200) {
            R.error("无法查找到订单行项目信息");
        }
        if (sumMoney.intValue() == -300) {
            R.error("收货记录中没有数量信息");
        }
        if (sumMoney.intValue() == -400) {
            R.error("表示订单行项目中没有单价信息");
        }
        return R.ok(sumMoney);
    }

    /**
     * 审核对账单
     */
    @SysLog("审核对账单")
    @RequestMapping("/checkBills")
    public R checkBills(String pkId, boolean agree) {
        ScmBillsHeaderEntity billsHeader = this.scmBillsHeaderService.selectById(pkId);
        int status = billsHeader.getBillsStatus();
        if (status > 0 || (status < 0 && !agree)) {
            return R.error("已经审核过了");
        }
        status--;
        status = agree ? Math.abs(status) : status;
        billsHeader.setBillsStatus(status);
        this.scmBillsHeaderService.updateById(billsHeader);
        //更改stock状态
        int st=6;
        if(!agree){
            st=-6;
        }
        this.scmBillsService.updateStockStatus(billsHeader.getBillNo(),st);
        List<ScmBillsEntity> billsList = this.scmBillsService.selectByMap(new HashMap<String, Object>() {
            private static final long serialVersionUID = 8494075354628393522L;

            {
                this.put("bill_no", billsHeader.getBillNo());
            }
        });
        String userName = ShiroUtils.getUserEntity().getUsername();
        Date updateDate = new Date();
        for (ScmBillsEntity bills : billsList) {
            bills.setBillStatus(billsHeader.getBillsStatus());
            bills.setUpdatePeople(userName);
            bills.setUpdateTime(updateDate);
            this.scmBillsService.updateById(bills);
        }
        return R.ok();
    }

}
